#!/usr/bin/env python3
import os
import sys
import argparse
from douyin_processor import DouyinProcessor
import shutil

def process_local_file(input_file, output_dir="processed_audio"):
    """Process a local audio file using our existing pipeline
    
    Args:
        input_file: Path to the input audio file
        output_dir: Output directory for processed files
    """
    if not os.path.exists(input_file):
        print(f"Error: File {input_file} does not exist")
        return None
        
    print(f"Processing local file: {input_file}")
    
    # Create output directory
    if not os.path.exists(output_dir):
        os.makedirs(output_dir)
        
    # Initialize processor
    processor = DouyinProcessor(output_dir)
    
    # Copy the input file to the raw directory
    base_name = os.path.basename(input_file).split('.')[0]
    raw_dir = os.path.join(output_dir, "raw")
    if not os.path.exists(raw_dir):
        os.makedirs(raw_dir)
        
    target_file = os.path.join(raw_dir, os.path.basename(input_file))
    
    # Only copy if needed
    if input_file != target_file:
        shutil.copy2(input_file, target_file)
        print(f"Copied input file to {target_file}")
    
    # Extract vocals
    vocals_file = processor.extract_vocals(target_file, base_name)
    if not vocals_file:
        print("Failed to extract vocals")
        return None
        
    # Segment the vocals
    segments = processor.segment_audio(vocals_file, base_name)
    if segments:
        print(f"Processed file. {len(segments)} segments saved to {processor.segments_dir}")
        return segments
    else:
        print("Failed to segment audio")
        return None

def main():
    parser = argparse.ArgumentParser(description="Process a local audio file")
    parser.add_argument("--input", required=True, help="Path to input audio file")
    parser.add_argument("--output_dir", default="processed_audio", help="Output directory")
    
    args = parser.parse_args()
    
    process_local_file(args.input, args.output_dir)

if __name__ == "__main__":
    main() 